/*
 * Copyright (c) 2006, 2021, webrx.cn All rights reserved.
 *
 */
package cn.webrx.wxs.controller;

import cn.webrx.wxs.entity.GoodsCategory;
import cn.webrx.wxs.mapper.GoodsCategoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>Project: wxshop - AdminGoodsCategoryController
 * <p>Powered By webrx
 * <p>Created By IntelliJ IDEA On 2021-04-28 16:47:41
 * <p>Description :
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 15
 */
@Controller
@RequestMapping("/admin")
public class AdminGoodsCategoryController {
    @Autowired
    private GoodsCategoryMapper gcdao;

    /**
     * <P>描述：</P>
     * @param m
     * @return
     */
    @GetMapping("goodsCategory/lists")
    public String lists(Model m) {
        //分配商品类别数据
        m.addAttribute("cate", gcdao.queryGoodsCate());
        return "admin/goods_category/lists";
    }

    @GetMapping("goodsCategory/add")
    public String add(Model m) {
        //分配商品类别数据
        m.addAttribute("cate", gcdao.showAddCate());
        return "admin/goods_category/add";
    }

    @ResponseBody
    @PostMapping("goodsCategory/asave")
    public Map<String, Object> asave(GoodsCategory cate) {
        Map<String, Object> map = new HashMap<String, Object>();
        if (cate.getPid() == 0) {
            cate.setLevel(1);
        } else {
            cate.setLevel(gcdao.getLevel(cate.getPid()));
        }
        if (gcdao.save(cate) == 0) {
            map.put("msg", "添加失败");
            map.put("code", 0);
        } else {
            map.put("msg", "添加成功");
            map.put("code", 1);
        }
        return map;
    }


    @ResponseBody
    @GetMapping("goodsCategory/state")
    public Map<String, Object> swtichUpdateState(int id, int status) {
        Map<String, Object> map = new HashMap<String, Object>();
        if (gcdao.updateSwitchState(id, status) == 1) {
            map.put("msg", "设置完成");
            map.put("code", 1);
        } else {
            map.put("msg", "设置失败");
            map.put("code", 0);
        }
        return map;
    }

    @ResponseBody
    @PostMapping("goodsCategory/del")
    public Map<String, Object> swtichUpdateState(int ids) {
        Map<String, Object> map = new HashMap<String, Object>();
        if (gcdao.isDel(ids) == 0) {
            //可以删除
            if (gcdao.del(ids) == 1) {
                map.put("msg", "删除成功");
                map.put("code", 400);
            } else {
                map.put("msg", "删除失败");
                map.put("code", 1);
            }
        } else {
            //不能删除
            map.put("msg", "设置失败,此类下有子类，请删除子类后再删除");
            map.put("code", 1);
        }
        return map;
    }

    @GetMapping("goodsCategory/edit")
    public String lists(Model m,int id) {
        //分配商品类别数据
        m.addAttribute("cates", gcdao.showAddCate());
        m.addAttribute("cate", gcdao.queryById(id));
        return "admin/goods_category/edit";
    }

    /**
     * cate id 为新的你类id  ids 为要修改的那个类别的id
     * @param cate
     * @param ids
     * @return
     */
    @ResponseBody
    @PostMapping("goodsCategory/esave")
    public Map<String, Object> esave(GoodsCategory cate,int ids) {
        Map<String, Object> map = new HashMap<String, Object>();
        //控制根分类
        if(cate.getId() == 0){
            cate.setPid(0);
            cate.setLevel(1);
            cate.setId(ids);
        }else if(ids != cate.getId()){ //如果改变了类别的位置
            cate.setPid(cate.getId());
            cate.setId(ids);
            cate.setLevel(gcdao.getLevel(cate.getPid()));
        }
        if (gcdao.update(cate) == 1) {
                map.put("msg", "修改成功");
                map.put("code", 1);
        } else {
            //不能删除
            map.put("msg", "修改失败");
            map.put("code", 0);
        }
        return map;
    }
}
